import { enumStrokeColor } from '../theme'

const getStartModel = CircleNodeModel => {
  class StartModel extends CircleNodeModel {
    static extendKey = 'StartModel'
    constructor(data, graphModel) {
      const { text, properties = {} } = data
      // 文字在上方，默认在下方
      const isUp = properties.textPosition === 'up'
      if (typeof text === 'string') {
        data.text = {
          value: data.text,
          x: data.x,
          y: isUp ? data.y - 30 : data.y + 35,
        }
      }
      super(data, graphModel)
    }
    getConnectedTargetRules() {
      const rules = super.getConnectedTargetRules()
      const notAsTarget = {
        message: '起始节点不能作为连线的终点',
        validate: () => false,
      }
      rules.push(notAsTarget)
      return rules
    }
  }
  return StartModel
}

const getStartNode = (CircleNode, h) => {
  class StartNode extends CircleNode {
    static extendKey = 'StartNode'
    getShape() {
      const { x, y, fill, stroke, strokeWidth, r, properties = {} } = this.getAttributes()
      const pStroke = enumStrokeColor[properties.type] || stroke
      return h(
        'g',
        {},
        h('circle', {
          cx: x,
          cy: y,
          fill,
          stroke: pStroke,
          strokeWidth,
          r,
        })
      )
    }
  }
  return StartNode
}

export const registerStartNode = ({ CircleNode, CircleNodeModel, h }) => ({
  model: getStartModel(CircleNodeModel),
  view: getStartNode(CircleNode, h),
})
